package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.ChannelN;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 满超
 * @date 2020/06/25
 **/
@Configuration
public class RabbitMqConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //配置序列化消息的类型为json
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    /**
     * 发送端消息可靠抵达设置
     */
    @PostConstruct//这个类创建以后执行这个方法
    public void initRabbitTemplate() {
        //设置发送端到exchange确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             * 只要消息安全抵达exchange服务器就会返回true
             * @param correlationData 消息唯一id
             * @param b 消息是否收到
             * @param s 失败的原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                if (b) {
                    System.out.println("消息安全抵达服务器");
                }
            }
        });

        //设置消息从exchange抵达队列确认回调
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            /**
             * 只要消息没有安全抵达，就触发这个失败回调
             * @param message 失败消息详细信息
             * @param i 回复的状态码
             * @param s 回复文本内容
             * @param s1 发送给那个交换机
             * @param s2 用的路由key
             */
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("失败的原因是：" + message);
            }
        });
    }


    /**
     * 注入mq配置,自动在rabbitmq创建
     */

    //死信队列
    @Bean
    public Queue orderDelayQueue() {
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","order-event-exchange");//死信交换机
        map.put("x-dead-letter-routing-key","order.release.order");//死信路由key
        map.put("x-message-ttl",60000);//60秒后收到
        return new Queue("order.delay.queue", true, false, false,map);
    }

    //普通队列
    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue", true, false, false);
    }

    //交换机
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange",true, false);
    }

    //绑定死信
    @Bean
    public Binding orderCreateOrder() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    //绑定普通
    @Bean
    public Binding orderReleaseOrder() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单和库存进行绑定
     */
    @Bean
    public Binding orderReleaseOther() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }
}
